import "package:flutter/material.dart";
import 'package:ichinesereader/controller/books.dart';
import 'package:ichinesereader/controller/schooladmin.dart';
import 'package:ichinesereader/utils/log.dart';
import 'package:ichinesereader/widgets/buttons/TextbookBar.dart';
import 'package:ichinesereader/widgets/grids/textbookGrid.dart';
import 'package:get/get.dart';
import '../../models/book/BookItem.dart';

class TextbookTabController extends StatefulWidget {
  TextbookTabController({
    Key? key,
  }) : super(key: key);

  _TextbookTabController createState() => new _TextbookTabController();
}

class _TextbookTabController extends State<TextbookTabController> {
  final BooksController _booksController = Get.find();
  final SchoolAdminController _schooladminController = Get.find();
  List<String> _selected = [];
  List<BookItem> _selectedBooklist = [];

  void _handleSelectChanged(List<String> newValue) {
    Log.debug(_selected.contains(newValue));
    setState(() {
      _selected = newValue;
    });
  }

  void _handleSelectChanged2(List<BookItem> newValue) {
    Log.debug(_selectedBooklist.contains(newValue));
    setState(() {
      _selectedBooklist = newValue;
      _booksController.setSelectedTextBoookItems(newValue);
    });
  }

  @override
  Widget build(BuildContext context) {
    Log.debug('starting build textbook tab');
    return Column(
      crossAxisAlignment: CrossAxisAlignment.stretch,
      children: [
        Container(
          // decoration: _pageStyles.pageTitleBoxDecoration(),
          height: 50,
          child: Obx(() {
            if (_schooladminController.classDetailsNew.value.program?.id !=
                null) {
              return TextbookBar(
                  selected: _selected, selectedBookList: _selectedBooklist);
            } else {
              return Visibility(child: Text(''), visible: false);
            }
          }),
        ),
        Flexible(
          child: _booksController.obx(
            (data) {
              if (_booksController.programBookList.isNotEmpty) {
                return TextbookGrid(
                  categories: _booksController.programBookList,
                  selected: _selected,
                  //selectedBooks: _selectedBooks,
                  onChanged: _handleSelectChanged,
                  onChanged2: _handleSelectChanged2,
                );
              } else {
                return Text('');
              }
            },
            onEmpty: Text('Coming soon ...'),
          ),
        ),
      ],
    );
  }
}
